package com.ruoyi.web.controller.TiebaPostCollectGuanzhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.controller.TiebaForums.domain.TiebaForums;
import com.ruoyi.web.controller.TiebaForums.mapper.TiebaForumsMapper;
import com.ruoyi.web.controller.TiebaPostCollectGuanzhu.vo.TiebaPostCollectVO;
import com.ruoyi.web.controller.TiebaPosts.domain.TiebaPosts;
import com.ruoyi.web.controller.TiebaPosts.mapper.TiebaPostsMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.web.controller.TiebaPostCollectGuanzhu.mapper.TiebaPostCollectGuanzhuMapper;
import com.ruoyi.web.controller.TiebaPostCollectGuanzhu.domain.TiebaPostCollectGuanzhu;
import com.ruoyi.web.controller.TiebaPostCollectGuanzhu.service.ITiebaPostCollectGuanzhuService;

/**
 * 帖子关注收藏表Service业务层处理
 *
 * @author yong
 * @date 2025-04-02
 */
@Service
public class TiebaPostCollectGuanzhuServiceImpl extends ServiceImpl<TiebaPostCollectGuanzhuMapper, TiebaPostCollectGuanzhu> implements ITiebaPostCollectGuanzhuService {

    @Autowired
    private TiebaPostCollectGuanzhuMapper tiebaPostCollectGuanzhuMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TiebaPostsMapper tiebaPostsMapper;

    @Autowired
    private TiebaForumsMapper tiebaForumsMapper;

    /**
     * 查询帖子关注收藏表列表
     *
     * @param tiebaPostCollectGuanzhu 帖子关注收藏表
     * @return 帖子关注收藏表
     */
    @Override
    public List<TiebaPostCollectGuanzhu> selectTiebaPostCollectGuanzhuList(TiebaPostCollectGuanzhu tiebaPostCollectGuanzhu) {
        List<TiebaPostCollectGuanzhu> tiebaPostCollectGuanzhuList = tiebaPostCollectGuanzhuMapper.selectList(buildQueryWrapper(tiebaPostCollectGuanzhu));
        return tiebaPostCollectGuanzhuList;
    }

    @Override
    public List<TiebaPostCollectVO> getTiebaPostCollectVOList(Long userId,Long forumType) {
        // 构建查询条件，默认查询类型为1（关注）
        QueryWrapper<TiebaPostCollectGuanzhu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("forum_type", forumType); // 默认查询类型为1
        queryWrapper.eq("user_id", userId); // 默认查询类型为1


        // 查询帖子关注收藏列表
        List<TiebaPostCollectGuanzhu> collectList = tiebaPostCollectGuanzhuMapper.selectList(queryWrapper);

        // 查询用户信息、帖子信息、贴吧信息和帖子创作者信息，并封装到 VO
        return collectList.stream().map(collect -> {
            // 查询用户信息
            SysUser user = sysUserMapper.selectUserById(collect.getUserId());
            // 查询帖子信息
            TiebaPosts post = tiebaPostsMapper.selectById(collect.getPostId());
            // 查询贴吧信息
            TiebaForums forum = tiebaForumsMapper.selectById(post.getForumId());
            // 查询帖子创作者信息
            SysUser postCreator = sysUserMapper.selectUserById(post.getUserId());

            return new TiebaPostCollectVO(
                    collect.getUserId(),
                    user.getNickName(),
                    user.getAvatar(),
                    collect.getPostId(),
                    post.getTitle(),
                    post.getContent(),
                    post.getImageUrl(),
                    post.getForumId(),
                    forum.getName(),
                    collect.getForumType(),
                    post.getUserId(),
                    postCreator.getNickName(),
                    postCreator.getAvatar(),
                    post.getCommentCount(),
                    post.getLoveCount(),
                    post.getShareCount()
            );
        }).collect(Collectors.toList());
    }


    private LambdaQueryWrapper<TiebaPostCollectGuanzhu> buildQueryWrapper(TiebaPostCollectGuanzhu query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<TiebaPostCollectGuanzhu> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(TiebaPostCollectGuanzhu::getCreateTime);
        return lqw;
    }

}
